package pers.cz.testNg;

import org.laziji.commons.rereg.exception.RegexpIllegalException;
import org.laziji.commons.rereg.exception.TypeNotMatchException;
import org.laziji.commons.rereg.exception.UninitializedException;
import pers.cz.mapper.AutoTestCaseParamsCodeMapper;
import pers.cz.mapper.AutoTestCaseParamsRegularMapper;
import pers.cz.model.AutoTestCaseParamsCode;
import pers.cz.model.AutoTestCaseParamsRegular;
import pers.cz.regularexpression.ExpressionUtils;
import pers.cz.spring.SpringContextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: 正则表达式方式获取参数
 * @author: Cheng Zhi
 * @create: 2023-12-17 18:34
 **/
public class RegularExprMethodJudgeImpl implements IMethodJudge {
    @Override
    public List<String[]> splitParam(Integer paramId, String params) {

        List<String[]> resultList = new ArrayList<>();
        String[] stringArray = new String[1]; // 初始化定义为1，如果需要再扩容
        // 1、查询数据库
        AutoTestCaseParamsRegular autoTestCaseParamsRegular = SpringContextUtil.getBean(AutoTestCaseParamsRegularMapper.class).selectByPrimaryKey(paramId);
        // 2、获取代码相关参数
        String expr = autoTestCaseParamsRegular.getExpr();

        try {
            String value = ExpressionUtils.random(expr);
            stringArray[0] = value;
            resultList.add(stringArray);
        } catch (RegexpIllegalException e) {
            e.printStackTrace();
        } catch (TypeNotMatchException e) {
            e.printStackTrace();
        } catch (UninitializedException e) {
            e.printStackTrace();
        }
        return resultList;
    }
}
